package com.blue.blog.api.controller;

import com.blue.blog.api.pojo.Blog;
import com.blue.blog.api.pojo.Result;
import com.blue.blog.api.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class BlogController {

    @Autowired
    private BlogService service;

    @GetMapping("/blogs")
    public Result findAllBlog() {
        List<Blog> blogs = service.findAllBlog();

        return Result.ok("查询成功!", blogs);
    }

    @GetMapping("/blog")
    public Result findBlogById(@RequestParam Long id) {
        Blog blog = service.findBlogById(id);

        return Result.ok("查询成功!", blog);
    }

    @PostMapping("/blog")
    public Result saveBlog(@RequestBody Blog blog) {
        Integer result = service.saveBlog(blog);

        return Result.ok("添加成功!", result);
    }

    @PutMapping("/blog")
    public Result updateBlog(@RequestBody Blog blog) {
        Integer result = service.updateBlog(blog);

        return Result.ok("更新成功!", result);
    }

    @DeleteMapping("/blog")
    public Result deleteBlogById(@RequestParam Long id) {
        Integer result = service.deleteBlogById(id);

        return Result.ok("删除成功!", result);
    }
}
